package com.fhao.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fhao.feign.PayServiceFeign;
import com.fuhao.base.BaseApiService;
import com.fuhao.base.ResponseBase;
import com.fuhao.constants.Constants;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Controller
public class PayController {

	@Autowired
	private PayServiceFeign payServiceFeign;
	
	/**
	 * 支付宝支付接口
	 * @throws IOException 
	 */
	@RequestMapping("/aliPay")
	public void aliPay(String payToken,HttpServletResponse response) throws IOException {
		//不设置无法渲染
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		if(StringUtils.isEmpty(payToken)) {
			//错误的话直接返回错误信息，或者也可以重定向到设定错误的页面
			writer.println("payToken不能为空");
			return ;
		}
		//远程调用支付api接口
		ResponseBase payTokenResult = payServiceFeign.findPayToken(payToken);
		if(!payTokenResult.getCode().equals(Constants.HTTP_RES_CODE_200)) {
			String mString = payTokenResult.getMsg();
			//错误的话直接返回错误信息，或者也可以重定向到设定的页面
			writer.println(mString);
			return ;
		}
		//获取可执行html元素
		LinkedHashMap map = (LinkedHashMap) payTokenResult.getData();
		String html = (String) map.get("payHtml");
		log.info("###########hmlt="+html);
		//执行html元素
		writer.println(html);
		writer.close();
	}
	
}
